<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-11-09 13:21:06
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Pro;


use App\Http\Services\Service;
use App\Repositories\ProColorApplyRepository;
use App\Repositories\ProColorApplyDetailsRepository;
use App\Repositories\ProColorRepository;
use Illuminate\Support\Facades\Storage;

/**
 * @property ProColorApplyRepository $repository
 * @property ProColorApplyDetailsRepository $details
 * @property ProColorRepository $color
 */
class ColorApplyService extends Service
    {
    public function boot(ProColorApplyRepository $repository, ProColorApplyDetailsRepository $details, ProColorRepository $color)
        {
        $this->repository = $repository;
        $this->details    = $details;
        $this->color      = $color;
        }

    public function list()
        {
        $data = $this->details->condition(request())->getGrid();
        return $data;
        }
    public function info()
        {
        return $this->repository->condition(request())->getOne();
        }
    public function countApply()
        {
        return $this->details->whereStatus_id(0)->count();
        }
    protected function save()
        {
        $data = request()->toArray();

        $data = $this->repository->autosave($data);
        $data->details->filter(function ($v) use ($data)
            {
            $color_data  = [
                'code'         => 'X' . $v->id,
                'name'         => '待打样',
                'code_en_name' => $v->code_en_name,
                'code_en'      => $v->code_en,
                'material_id'  => $v->material_id,
                'company_id'   => $data->customer_id,
            ];
            $v->color_id = $this->color->create($color_data)->id;
            $v->save();
            });
        }
    protected function delete()
        {
        $details  = $this->details->whereIn("id", $this->getRequestIds())->get();
        $main_ids = [];
        $details->filter(function ($item) use (&$main_ids)
            {
            if ($item->color) {
                if ($item->color->code != 'X' . $item->id)
                    fail("该申请已打样！禁止删除！");
                $market = $item->color->market()->first();
                if ($market) {
                    fail("该申请已报色，单号[{$market['code']}],删除失败!");
                    }
                $item->color->delete();
                }
            $item->delete();
            $main_ids[$item->main_id] = $item->main_id;
            });
        //删除主单
        return $this->repository->whereIn("id", $main_ids)->get()->filter(function ($item)
            {
            if ($item->details->count() == 0) {
                $item->delete();
                }
            });
        }
    protected function saveColor()
        {
        $request                   = request();
        $gdata                     = $this->details->where("id", $request->id)->first();
        $color_data                = $gdata->color;
        $color_data->name          = $request->name;
        $color_data->dyebath_id    = $request->dyebath_id;
        $color_data->colorgroup_id = $request->colorgroup_id;
        $color_data->code          = '';
        $color_data->generateCode('COLOR', 'code', $request->colorgroup_id);
        // dd($color_data);
        $color_data->save();
        $gdata->status_id = 1;
        return $gdata->save();
        }

    }